package com.epfresh.supplier.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.epfresh.api.utils.LocalDisplay;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 2017/12/12
 * @Description
 * @Project EpfreshSupplier
 * @Package com.epfresh.supplier.widget
 * @Copyright personal
 */


public class NumberHorizontalProgressBar extends ProgressBar {

    Paint mPaint;

    public NumberHorizontalProgressBar(Context context) {
        super(context);
        initText();
    }

    public NumberHorizontalProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initText();
    }

    public NumberHorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initText();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public NumberHorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        initText();
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {

        Rect rect = new Rect();
        String text = getText();
        int widthView = getWidth();
        if (text != null && text.length() > 0) {
            this.mPaint.getTextBounds(text, 0, text.length(), rect);

            int progress = getProgress();
            int wr = progress * widthView / getMax();
            int width = rect.width();

            if (width > wr) {
                int p = (width + LocalDisplay.dp2px(12)) * getMax() / widthView + 1;
                super.setProgress(p);
                return;
            }



            int x = (wr / 2) - rect.centerX();

            int y = (getHeight() / 2) - rect.centerY();
            super.onDraw(canvas);
            canvas.drawText(text, x, y, this.mPaint);
        }else {
            this.mPaint.getTextBounds("0%", 0, "0%".length(), rect);

            int progress = 100;
            int wr = progress * widthView / getMax();

            int x = (wr / 2) - rect.centerX();

            int y = (getHeight() / 2) - rect.centerY();
            super.onDraw(canvas);
            canvas.drawText("0%", x, y, this.mPaint);
        }


    }


    //初始化，画笔
    private void initText() {
        this.mPaint = new Paint();
        this.mPaint.setColor(Color.WHITE);
        this.mPaint.setAntiAlias(true);
        this.mPaint.setTextSize(LocalDisplay.dp2px(12));
    }

    @Override
    public synchronized void setProgress(int progress) {
        mProgressReal = progress;
        if (progress > 0 && progress < 15) {
            progress = 15;
        }

        if (progress == 0){
            super.setSecondaryProgress(100);
        }else {
            super.setSecondaryProgress(0);
        }
        super.setProgress(progress);

    }


    int mProgressReal;

    public int getProgressReal() {
        return mProgressReal;
    }

    public void setProgressReal(int mProgressReal) {
        this.mProgressReal = mProgressReal;
    }


    private String getText() {
        if (mProgressReal > 0) {
            int i = (mProgressReal * 100) / this.getMax();
            return String.valueOf(i) + "%";
        }
        return null;
    }
}
